package com.ewanghuiju.app.ui.web;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;

import com.ewanghuiju.app.util.LogUtil;
import com.google.gson.Gson;
import com.tencent.smtt.export.external.interfaces.JsPromptResult;
import com.tencent.smtt.sdk.ValueCallback;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebView;

public class JsUtils {
    private static final String TAG = "JsUtils";
    private static String scheme = "hfjssdk";
    private static String authority = "webview";
    private static String replace = "hfjssdk://webview?param=";
    private static String key = "param";
    private static String APP_NAME_UA = "app_ewhj";

    public static void send(JsPromptResult result, JsDataBean s) {
//        if (result != null) {
//            result.confirm(JSON.toJSONString(s));
//        }
    }

    public static void sendH5(X5WebView x5WebView, JsDataBean s) {
        // Android版本变量
        int version = Build.VERSION.SDK_INT;
        // 因为该方法在 Android 4.4 版本才可使用，所以使用时需进行版本判断
//        String ss = JSON.toJSONString(s);
        String ss = new Gson().toJson(s);
        if (version < 18) {
            x5WebView.loadUrl("javascript:hf_sendToH5(" + ss + ")");
        } else {
            x5WebView.evaluateJavascript("javascript:hf_sendToH5(" + ss + ")", new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String value) {
                    //此处为 js 返回的结果
                    LogUtil.e(value);
                }
            });
        }
    }

    public static void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
        FilesChooseUtils.onActivityResult(activity, requestCode, resultCode, data);
    }

    public static void register(Activity activity, MyWebViewClient myWebViewClient, OnListener listener) {
        X5WebView x5WebView = myWebViewClient.getWebView();

//        String str = x5WebView.getSettings().getUserAgentString();
//        if (str.indexOf(APP_NAME_UA) == -1) {
//            x5WebView.getSettings().setUserAgentString(x5WebView.getSettings().getUserAgentString() + APP_NAME_UA);
//        }

        x5WebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView webView, String s) {
                super.onReceivedTitle(webView, s);
//                x5WebView.titleChange(s);
            }

            @Override
            public void onProgressChanged(WebView webView, int i) {
                super.onProgressChanged(webView, i);
//                myWebViewClient.setProgress(i);
            }

            @Override
            public void openFileChooser(ValueCallback<Uri> valueCallback, String s, String s1) {
                FilesChooseUtils.openFileChooser(activity, valueCallback);
            }

            @Override
            public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> valueCallback, FileChooserParams fileChooserParams) {
                FilesChooseUtils.onShowFileChooser(activity, valueCallback);
                return true;
            }

//            @Override
//            public boolean onJsPrompt(WebView webView, String s0, String url, String s2, JsPromptResult result) {
//                LogUtil.e("url:" + s0 + ",msg:" + url);
//                if (url.indexOf(scheme) != -1) {
//                    Uri uri = Uri.parse(url);
//                    if (uri.getScheme().equals(scheme)) {
//                        if (uri.getAuthority().equals(authority)) ;
//                        {
//
//                            if (listener != null) {
//                                String str = url.replace(replace, "");
//                                async(result, str, listener);
//                            }
//
//
//                        }
//                    }
//                    return true;
//                }
//                return super.onJsPrompt(webView, s0, url, s2, result);
//            }
        });
    }


    public static void async(JsPromptResult result, String str, OnListener listener) {
//        JsDataBean json = JSON.parseObject(str, new TypeReference<JsDataBean>() {
//        });
//        String data = json.getData();
//        int type = json.getType();
//        if (listener != null) {
//            switch (type) {
//                case 0:
//                    listener.onTest(result, type, data);
//                    break;
//                case 1:
//                    listener.onWxPay(result, type, data);
//                    break;
//                case 2:
//                    listener.onAliPay(result, type, data);
//                    break;
//                case 100:
//                    result.confirm("");
//                    listener.onDestoryActivity(result, type, data);
//                    break;
//                default:
//                    result.confirm("");
//                    break;
//            }
//        }

    }

    public static class OnListener {
        public void onTest(JsPromptResult result, int type, String json) {
            result.confirm("返回数据");
        }

        ;

        public void onWxPay(JsPromptResult result, int type, String json) {
            result.confirm("返回数据");
        }

        ;

        public void onAliPay(JsPromptResult result, int type, String json) {
            result.confirm("返回数据");
        }

        ;

        public void onDestoryActivity(JsPromptResult result, int type, String json) {
            result.confirm("返回数据");
        }

        ;
    }


}
